package com.itheima.学生管理系统;

import java.util.ArrayList;
import java.util.Scanner;

public class TestStudent {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        lo:while (true) {
            System.out.println("-------欢迎来到学生管理系统-------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择：");
            int num = sc.nextInt();
            if (num > 0 && num < 6) {
                switch (num) {
                    case 1:
                        addStudent(list);
                        break;
                    case 2:
                        delateStudent(list);
                        break;
                    case 3:
                        setStudent(list);
                        break;
                    case 4:
                        getStudent(list);
                        break;
                    case 5:
                        System.out.println("谢谢使用");
                        break lo;
                    default:
                        System.out.println("你输入的选项有误");
                        break;
                }
            } else {
                System.out.println("你输入的选项有误,请重新输入：");
            }
        }
    }

    private static void setStudent(ArrayList<Student> list) {
        System.out.println("请输入你要修改的学号：");
        Scanner sc = new Scanner(System.in);
        String s;
        while (true) {
            s = sc.next();
            if(getIndex(list, s) == -1) {
                System.out.println("该信息不存在，请重新输入：");
            }else {
                break;
            }
        }
        System.out.println("请输入新的姓名：");
        list.get(getIndex(list, s)).setName(sc.next());
        System.out.println("请输入新的年龄：");
        list.get(getIndex(list, s)).setAge(sc.nextInt());
        System.out.println("请输入新的生日：");
        list.get(getIndex(list, s)).setBirthday(sc.next());
        System.out.println("修改成功");
        return;

    }

    private static void delateStudent(ArrayList<Student> list) {
        System.out.println("请输入你要删除的学号：");
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        if(getIndex(list, s) == -1) {
            System.out.println("该信息不存在，请重新输入:");
            s = sc.next();
        }else {
            list.remove(getIndex(list, s));
            System.out.println("删除成功");
            return;
        }
    }

    private static void getStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("无信息，请添加后重新查询");
            return;
        }
        System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getId() + "\t\t" + list.get(i).getName() + "\t\t" + list.get(i).getAge() + "\t\t" + list.get(i).getBirthday());
        }
    }

    private static int getIndex(ArrayList<Student> list, String id) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if(id.equals(list.get(i).getId())) {
                   index = i;
            }
        }
        return index;
    }

    private static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        Student s = new Student();
        System.out.println("请输入学号：");
        String id = sc.next();
        if(getIndex(list,id) != -1) {
            System.out.println("你输入的id已存在，请重新输入：");
            id = sc.next();
        }
        s.setId(id);
        System.out.println("请输入姓名：");
        s.setName(sc.next());
        System.out.println("请输入年龄：");
        s.setAge(sc.nextInt());
        System.out.println("请输入生日：");
        s.setBirthday(sc.next());
        list.add(s);
        System.out.println("添加成功");
    }
}
